package com.nanhai.buddhist.college.module.study.controller.admin.studyevaluationitem;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.annotations.*;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import com.nanhai.buddhist.college.framework.common.pojo.PageResult;
import com.nanhai.buddhist.college.framework.common.pojo.CommonResult;
import static com.nanhai.buddhist.college.framework.common.pojo.CommonResult.success;

import com.nanhai.buddhist.college.framework.excel.core.util.ExcelUtils;

import com.nanhai.buddhist.college.framework.operatelog.core.annotations.OperateLog;
import static com.nanhai.buddhist.college.framework.operatelog.core.enums.OperateTypeEnum.*;

import com.nanhai.buddhist.college.module.study.controller.admin.studyevaluationitem.vo.*;
import com.nanhai.buddhist.college.module.study.dal.dataobject.studyevaluationitem.StudyEvaluationItemDO;
import com.nanhai.buddhist.college.module.study.convert.studyevaluationitem.StudyEvaluationItemConvert;
import com.nanhai.buddhist.college.module.study.service.studyevaluationitem.StudyEvaluationItemService;

@Api(tags = "管理后台 - 评价项目")
@RestController
@RequestMapping("/study/evaluation-item")
@Validated
public class StudyEvaluationItemController {

    @Resource
    private StudyEvaluationItemService evaluationItemService;

    @PostMapping("/create")
    @ApiOperation("创建评价项目")
//    @PreAuthorize("@ss.hasPermission('study:evaluation-item:create')")
    public CommonResult<String> createEvaluationItem(@Valid @RequestBody StudyEvaluationItemCreateReqVO createReqVO) {
        return success(evaluationItemService.createEvaluationItem(createReqVO));
    }

    @PutMapping("/update")
    @ApiOperation("更新评价项目")
//    @PreAuthorize("@ss.hasPermission('study:evaluation-item:update')")
    public CommonResult<Boolean> updateEvaluationItem(@Valid @RequestBody StudyEvaluationItemUpdateReqVO updateReqVO) {
        evaluationItemService.updateEvaluationItem(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @ApiOperation("删除评价项目")
    @ApiImplicitParam(name = "id", value = "编号", required = true, dataTypeClass = String.class)
//    @PreAuthorize("@ss.hasPermission('study:evaluation-item:delete')")
    public CommonResult<Boolean> deleteEvaluationItem(@RequestParam("id") String id) {
        evaluationItemService.deleteEvaluationItem(id);
        return success(true);
    }

    @GetMapping("/get")
    @ApiOperation("获得评价项目")
    @ApiImplicitParam(name = "id", value = "编号", required = true, example = "1024", dataTypeClass = String.class)
//    @PreAuthorize("@ss.hasPermission('study:evaluation-item:query')")
    public CommonResult<StudyEvaluationItemRespVO> getEvaluationItem(@RequestParam("id") String id) {
        StudyEvaluationItemDO evaluationItem = evaluationItemService.getEvaluationItem(id);
        return success(StudyEvaluationItemConvert.INSTANCE.convert(evaluationItem));
    }

    @GetMapping("/list-byids")
    @ApiOperation("获得评价项目列表")
    @ApiImplicitParam(name = "ids", value = "编号列表", required = true, example = "1024,2048", dataTypeClass = List.class)
//    @PreAuthorize("@ss.hasPermission('study:evaluation-item:query')")
    public CommonResult<List<StudyEvaluationItemRespVO>> getEvaluationItemList(@RequestParam("ids") Collection<String> ids) {
        List<StudyEvaluationItemDO> list = evaluationItemService.getEvaluationItemList(ids);
        return success(StudyEvaluationItemConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @ApiOperation("获得评价项目分页")
//    @PreAuthorize("@ss.hasPermission('study:evaluation-item:query')")
    public CommonResult<PageResult<StudyEvaluationItemRespVO>> getEvaluationItemPage(@Valid StudyEvaluationItemPageReqVO pageVO) {
        PageResult<StudyEvaluationItemDO> pageResult = evaluationItemService.getEvaluationItemPage(pageVO);
        return success(StudyEvaluationItemConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @ApiOperation("导出评价项目 Excel")
//    @PreAuthorize("@ss.hasPermission('study:evaluation-item:export')")
    @OperateLog(type = EXPORT)
    public void exportEvaluationItemExcel(@Valid StudyEvaluationItemExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<StudyEvaluationItemDO> list = evaluationItemService.getEvaluationItemList(exportReqVO);
        // 导出 Excel
        List<StudyEvaluationItemExcelVO> datas = StudyEvaluationItemConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "评价项目.xls", "数据", StudyEvaluationItemExcelVO.class, datas);
    }


    @GetMapping("/list")
    @ApiOperation("获得评价项目列表")
    public CommonResult<List<StudyEvaluationItemRespVO>> getEvaluationItemsList(@Valid StudyEvaluationItemExportReqVO queryVo) {
        List<StudyEvaluationItemDO> list = evaluationItemService.getEvaluationItemList(queryVo);
        return success(StudyEvaluationItemConvert.INSTANCE.convertList(list));
    }

}
